package com.bbcare.cp.plat.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.bbcare.comm.AppException;
import com.bbcare.comm.ConstantParams;
import com.bbcare.comm.Constants;
import com.bbcare.comm.ErrorCode;
import com.bbcare.comm.ToolUtil;
import com.bbcare.cp.plat.entity.au.AuHospital;
import com.bbcare.cp.plat.entity.au.AuTenantDeptRel;
import com.bbcare.cp.plat.service.IAuTenantService;
import com.bbcare.cp.plat.store.ibatis.au.IAuHospitalDAO;
import com.bbcare.cp.plat.store.ibatis.au.IAuTenantDAO;

@Service
public class AuTenantServiceService implements IAuTenantService {

    Logger logger = Logger.getLogger(AuTenantServiceService.class);

    @Autowired
    public IAuHospitalDAO auHospitalDAO;

    @Autowired
    public IAuTenantDAO auTenantDAO;

	@Override
	public AuHospital qryHospitalInfoByDoctorId(Map<String, Object> qryMap) throws Exception {
		AuHospital hospital = auHospitalDAO.qryHospitalInfoByDoctorId(qryMap);
		return hospital;
	}


	@Override
	public AuHospital qryHospitalById(Map<String, Object> param)
			throws Exception {
		String hospitalId = ToolUtil.getStringFromMap(param, "hospitalId");
		Map<String,Object> qryMap = new HashMap<String,Object>();

		qryMap.put("hospitalId", hospitalId);
		AuHospital hospital = auHospitalDAO.selectHospital(qryMap);
		return hospital;
	}


	@Override
	public AuHospital qryHospitalByDepartmentId(Map<String, Object> param)
			throws Exception {
		String departmentId = ToolUtil.getStringFromMap(param, "departmentId");
		Map<String,Object> qryMap = new HashMap<String,Object>();

		qryMap.put("departmentId", departmentId);
		AuHospital hospital = auHospitalDAO.qryHospitalInfoByDepartmentId(qryMap);
		return hospital;
	}

	/* (non-Javadoc)
	 * @see com.bbcare.wechat.plat.service.IWeChatService#qryHospitalDetail(java.util.Map)
	 */
	@Override
	public AuHospital qryHospitalDetailByTenant(Map<String, Object> paramMap)
			throws Exception {
		String tenantId = ToolUtil.getStringFromMap(paramMap,"tenantId");
		String relationType = ConstantParams.TENANT_REL_TYPE_1;//默认按医院  根据租户查询

		Map<String,Object> qryMap = new HashMap<String,Object>();
		qryMap.put("tenantId", tenantId);
		qryMap.put("relationType", relationType);
		AuTenantDeptRel auTenantDeptRel  = qryAuTenantRel(qryMap);
		if(null == auTenantDeptRel){
			throw new AppException(ErrorCode.WECHAT_EXCEPTION_QRCODE_INFO_NOT_EXISTS,"未配置医院信息");
		}
		String hospitalId = auTenantDeptRel.getRelationId();
		qryMap.clear();
		qryMap.put("hospitalId", hospitalId);

		AuHospital hospital = qryHospitalInfoById(qryMap);
		return hospital;
	}

    /*
     * (non-Javadoc)
     *
     * @see
     * com.bbcare.cp.plat.service.IAuHospitalService#qryHospitalInfoById(java.
     * util.Map)
     */
    @Override
    public AuHospital qryHospitalInfoById(Map<String, Object> param) throws Exception {
        String hospitalId = ToolUtil.getStringFromMap(param, "hospitalId");
        Map<String, Object> qryMap = new HashMap<String, Object>();

        qryMap.put("hospitalId", hospitalId);
        AuHospital hospital = auHospitalDAO.selectHospital(qryMap);
        List<Map<String, Object>> address = auHospitalDAO.selectHospitalAddress(qryMap);
        if (!CollectionUtils.isEmpty(address)) {
            hospital.setAddressList(address);
        }
        return hospital;
    }

    /*
     * (non-Javadoc)
     *
     * @see
     * com.bbcare.cp.plat.service.IAuTenantService#qryAuTenantRel(java.util.Map)
     */
    @Override
    public AuTenantDeptRel qryAuTenantRel(Map<String, Object> param) throws Exception {
        String tenantId = ToolUtil.getStringFromMap(param, "tenantId");
        String relationType = ToolUtil.getStringFromMap(param, "relationType");
        Map<String, Object> qryMap = new HashMap<String, Object>();
        qryMap.put("tenantId", tenantId);
        qryMap.put("relationType", relationType);
        qryMap.put("status", Constants.COMMON_STATUS_NOMAL);
        List<AuTenantDeptRel> list = auTenantDAO.selectTenantDeptRel(qryMap);
        if (!CollectionUtils.isEmpty(list)) {
            return list.get(0);
        } else {
            return null;
        }

    }
}
